package com.xzsoft.gdwz.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xzsoft.gdwz.model.TrdPsEmInfLog;
import com.xzsoft.gdwz.services.TrdPsEmInfLogService;
import com.xzsoft.gdwz.util.Tools;

/**
 * 
 * <p>Title: 日志管理</p>
 * <p>Description: 使用mongoDB做日志的持久化</p>
 * <p>Company: 兴竹</p> 
 * @author:龙本华
 * @date:2016年8月30日 下午3:31:12
 */
@Controller
@RequestMapping("/log")
public class LogManagerController {
	private static final Logger logger = LoggerFactory .getLogger(LogManagerController.class); 
	
	/**
	 * dubbo提供的远程接口,由“e-commerce-log”工程提供服务
	 */
	@Autowired
	private TrdPsEmInfLogService trdPsEmInfLogService;
	
	/**
	 * 跳转日志管理界面
	 * @return
	 */
	@RequestMapping("/logManage")
	public String logList(){		
		return "logManage";
	}
	
	/**
	 * 添加一条日志数据，测试使用
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/addLog")
	public String addLog(HttpServletResponse response) throws IOException{
		TrdPsEmInfLog log = new TrdPsEmInfLog();
		log.setId(UUID.randomUUID().toString());
		log.setEmName("JD");
		DateTime dateTime = new DateTime();
		String createdate = dateTime.toString("yyyy-MM-dd HH:mm:ss");
		log.setCreateDate(createdate);
		log.setErrorMsg("null pointexception");
		log.setHasRecieve("0");
		log.setOrderId(Tools.getRandomLong()+"");
		log.setOrderInfo(Tools.getRandomString(10000));
		JSONObject json = JSONObject.fromObject(log);
		trdPsEmInfLogService.insertTrdPsEmInfLog(log);
		response.getWriter().println(json.toString());
		response.getWriter().flush();  
		response.getWriter().close(); 
		return null;
	}
	
	/**
	 * 获取日志列表
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getList")
	public void getList(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String emName = request.getParameter("ename");
		String orderId = request.getParameter("orderId");
		String createDate = request.getParameter("createDate");
		if(createDate != null && !"".equals(createDate)){
			DateTime dateTime = DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(createDate);
			createDate = dateTime.toString("yyyy-MM-dd HH:mm:ss");
		}
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		logger.info("page:"+page+"  rows:"+rows);
		TrdPsEmInfLog log = new TrdPsEmInfLog();
		log.setEmName(emName);
		log.setOrderId(orderId);
		log.setCreateDate(createDate);
		log.setPage(page);
		log.setRows(rows);
		String result = trdPsEmInfLogService.findByCondition(log);
		response.getWriter().println(result);
		response.getWriter().flush();  
		response.getWriter().close(); 
	}
	
	/**
	 * 删除日志，参数为用逗号链接的日志id
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/removeLog")
	public void removeLog(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String ids = request.getParameter("ids");
		logger.info("删除的日志ID:"+ids);
		JSONObject obj = new JSONObject();
		try {
			trdPsEmInfLogService.removeLog(ids);
			obj.put("flag", 1);
			obj.put("errorMsg", "");
		} catch (Exception e) {
			obj.put("flag", 0);
			obj.put("errorMsg", e.getMessage());
		}
		response.getWriter().println(obj.toString());
		response.getWriter().flush();  
		response.getWriter().close(); 
	}
}
